#import "FlkuaishouPlugin.h"
#import <KwaiSDK/KSApi.h>

static FlutterMethodChannel* channel;

@interface FlkuaishouPlugin() <KSApiDelegate>
@end

@implementation FlkuaishouPlugin
+ (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar>*)registrar {
  channel = [FlutterMethodChannel
      methodChannelWithName:@"flutter_kuaishou"
            binaryMessenger:[registrar messenger]];
  FlkuaishouPlugin* instance = [[FlkuaishouPlugin alloc] init];
  [registrar addMethodCallDelegate:instance channel:channel];
}

- (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
  if ([@"getPlatformVersion" isEqualToString:call.method]) {
    result([@"iOS " stringByAppendingString:[[UIDevice currentDevice] systemVersion]]);
  }else if([@"initPlatform" isEqualToString:call.method]){//注册快手
      NSDictionary* dictionary =  call.arguments;
      NSString* registerApp =  dictionary[@"registerApp"];
      NSString* universalLink =  dictionary[@"universalLink"];
      NSLog(@"registerApp=%@,universalLink=%@",registerApp,universalLink);
      
      [KSApi registerApp:registerApp universalLink:universalLink delegate:self];
      result(@"success");
  } else if([@"kuaishouOpenSDKAuthRequest" isEqualToString:call.method]){
      NSLog(@"kuaishouOpenSDKAuthRequest");
     
      
      KSAuthRequest *reqsss= [[KSAuthRequest alloc] init];
//      reqsss.authType = @"code";
      reqsss.scope = @"user_info,user_base,user_video_delete,user_video_publish,user_video_info";
      reqsss.h5AuthViewController = self;
      [KSApi sendRequest:reqsss completion:^(BOOL success) {
          if(success){
              result(@"success");
          }else{
              result(@"error");
          }
         
      }];
      
  }else {
    result(FlutterMethodNotImplemented);
  }
}
- (void)ksApiDidReceiveResponse:(__kindof KSAuthResponse *)response{
    NSLog(@"返回：%@",response.code);
    NSMutableDictionary *dyAuthDic = [[NSMutableDictionary alloc] init];
    [dyAuthDic setValue:response.code forKey:@"code"];
    NSLog(@"授权成功返回：%@",dyAuthDic);
    [channel invokeMethod:@"ISO_KuaiShouOpenSDKAuthRequest" arguments:dyAuthDic];
}
@end
